<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  
 <%@ page import="dal.LocationDataService" %>
<%@ page import="dao.dto.LocationDTO" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.TreeMap" %>
<%@ page import="java.util.Set" %>
<%@page import="java.util.HashMap"%>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="ajax" uri="/WEB-INF/tlds/ajax_controls.tld"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New York City Listing</title>
	<ajax:page/>
	<link href="core.css" rel="stylesheet" type="text/css" />

</head>
<script type="text/javascript">
	function showCityDialog(zipCode) {
		var xPosition = 0;
		var yPosition = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			yPosition = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			xPosition = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			yPosition = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		
		// position dialog where at cursor
		var dialogControl = document.getElementById("cityDetailsDialog");
		if (dialogControl != null) {
			dialogControl.style.top = yPosition;
			dialogControl.style.left = xPosition;
		}
		
		// Get selected zip code
		var zipCodeControl = document.getElementById("selectedZipCode");

		if (zipCodeControl != null) {
			zipCodeControl.value = zipCode;
			showDialog('cityDetailsDialog');
		}
	}

	
</script>
<body>

<table cellpadding="0" cellspacing="0" width="60%" >	
		<tbody>
			<%
			
			TreeMap<Integer, LocationDTO> locData = LocationDataService.getLocationData();
	
			// Temp map to ensure only unique cities are added. If a city spans
			// multiple zip codes, multiple location objects will be returned from the
			// Location Data Service. We were want to filter those duplicates out.
			HashMap tempMap = new HashMap(); 
			// Iterate through all data looking for matching cities and add to temporary TreeMap
			Set<Integer> keySet = locData.keySet();
			Iterator<Integer> locIter = keySet.iterator();		
			while (locIter.hasNext()) {
		    	// Get current state
				Integer curKey = locIter.next();
				LocationDTO curLocation = locData.get(curKey);
				
				String curState = curLocation.getState();
				
				String curCity = curLocation.getCity();
				String lowerCurCity = curCity.toLowerCase();
				
				if (!tempMap.containsKey(curCity) && curState.equalsIgnoreCase("NY")) {
					tempMap.put(curCity, curCity);
					out.println("<tr>");
					out.println("<td><a href='#' style='text-decoration:none;color' onMouseOver=\"showCityDialog('" + curLocation.getZipCode() + "')\" >");
					out.println(curLocation.getZipCode());
					out.println("</td>");
					out.println("<td>");
					out.println(curLocation.getCity());
					out.println("</td>");			
					out.println("<td>");
					out.println(curLocation.getCounty());
					out.println("</td>");
					out.println("</tr>");
				}
			}
			%>
		</tbody>
		<thead>
			<tr>
				<th colspan="3"><b>Cities</b></th>
			</tr>
			<tr>
				<th class="subHeading"><b>Zip Code</b></th>
				<th class="subHeading"><b>City</b></th>
				<th class="subHeading"><b>County</b></th>
			</tr>
		</thead>
	</table>

	<input type="hidden" id="selectedZipCode" value=""/>

	<ajax:popupdialog url="/ajaxcontrols3/fragments/cityDetails.jsp" 
		id="cityDetailsDialog" 
		width="400px" 
		left="0" 
		top="0" 
		title="City Information" 
		height="10.5em"
		contentheight="8.5em">
	
		<ajax:popuparguments id="stateDialogArgs">
			<ajax:popupargument name="zipCode" sourcefield="selectedZipCode"/>
		</ajax:popuparguments>

	</ajax:popupdialog>
</body>
</html>